package com.lancoo.skegndemo;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lancoo.skegn.LancooSkEgnManager;
import com.lancoo.skegn.model.SkegnObj;
import com.lancoo.skegndemo.dialog.AudioRecoderDialog;
import com.lancoo.skegndemo.dialog.AudioRecoderUtils;
import com.lancoo.skegndemo.utlis.JSONTool;

/**
 * 评测测试界面
 */
public class EvaluationActivity extends AppCompatActivity {

    private static final String TAG = "EvaluationActivity";
    private AudioRecoderDialog mRecoderDialog;
    private AudioRecoderUtils mRecoderUtils;
    protected RelativeLayout rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluation);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setHomeAsUpIndicator(R.drawable.back);
        supportActionBar.setHomeButtonEnabled(true);
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        findViewById(R.id.img_record).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        Log.e(TAG,"开始评测");
                        startRecoder();
                        return true;
                    case MotionEvent.ACTION_UP:
                        Log.e(TAG,"停止评测");
                        stopRecord(true);
                        return true;
                    case MotionEvent.ACTION_CANCEL:
                        Log.e(TAG,"取消评测");
                        stopRecord(false);
                        return true;

                }

                return false;
            }
        });
        rootView = findViewById(R.id.rootView);
        mRecoderUtils = new AudioRecoderUtils();
        mRecoderUtils.setOnAudioStatusUpdateListener(mOnAudioStatusUpdateListener);
        mRecoderDialog = new AudioRecoderDialog(this);
        mRecoderDialog.setShowAlpha(0.98f);

    }


    private void startRecoder(){
        EditText editText = findViewById(R.id.et_content);
        String content=editText.getText().toString().trim();
        if(TextUtils.isEmpty(content)){
            content=editText.getHint().toString();
        }
        LancooSkEgnManager.getInstance().startRecord(this, content, new LancooSkEgnManager.OnLancooRecordListener() {
            @Override
            public void onRecordStart() {
                Log.e(TAG,"onRecordStart");
            }

            @Override
            public void onRecording(int vad_status, int sound_intensity) {
                Log.e(TAG,"onRecording..."+vad_status+",..."+sound_intensity);

            }

            @Override
            public void onRecordEnd(int grade, String url, SkegnObj skegnResult) {
                Log.e(TAG,"onRecordEnd,"+url);
                String content="评测结果:"+"\n"+JSONTool.stringToJSON(new Gson().toJson(skegnResult));
                showRecordResult(content);
            }

            @Override
            public void onRecordError(String error) {
                Log.e(TAG,"onRecordError");
                showRecordResult("评测失败:"+"\n"+error);
            }
        });
        mRecoderUtils.startRecord();
        mRecoderDialog.showAtLocation(rootView, Gravity.CENTER, 0, 0);
    }

    private void stopRecord(boolean isShowScore){
        LancooSkEgnManager.getInstance().stopRecord(isShowScore);
        mRecoderUtils.stopRecord();
        if(mRecoderDialog!=null && mRecoderDialog.isShowing()){
            mRecoderDialog.dismiss();
        }
    }

    private void showRecordResult(String content){
        TextView tv_result = findViewById(R.id.tv_result);
        if (tv_result!=null){
            tv_result.setMovementMethod(ScrollingMovementMethod.getInstance());
            tv_result.setText( content);
        }
    }

    AudioRecoderUtils.OnAudioStatusUpdateListener mOnAudioStatusUpdateListener = new AudioRecoderUtils.OnAudioStatusUpdateListener() {
        @Override
        public void onUpdate(double db) {
            if (null != mRecoderDialog) {
                mRecoderDialog.setLevel((int) db);
            }
        }
    };


    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }

}
